<template>
  <div>
    <Table
      ref="tableRef"
      :url="URL_PAGINGQUERYPLATFORMAREA"
      :columns="data.columns"
      :handles="data.handles"
      :toolbar="data.toolbar"
      :query="data.query"
    ></Table>
  </div>
</template>

<script lang="ts" setup name="courtsList">
import Table from '@/components/table/index.vue';
import {
  URL_PAGINGQUERYPLATFORMAREA,
  delPlatformAreaById,
  afterDelPlatformAreaById,
} from '@/service/apis/pile';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

const data = reactive({
  columns: [
    {
      prop: 'id',
      label: '台区id',
    },
    {
      prop: 'platformAreaName',
      label: '台区名称',
    },
    {
      prop: 'platformAreaCode',
      label: '台区编码',
    },
    {
      prop: 'equipmentCount',
      label: '关联电桩数',
      dataType: String,
    },
  ],
  handles: [
    {
      label: '修改',
      permission: 'courtsList.edit',
      handle: (row: any) => {
        console.log('编辑---', row);
        router.push({
          name: 'courtsEdit',
          query: { id: row.id },
        });
      },
    },
    {
      label: '关联电桩',
      permission: 'courtsList.see',
      handle: (row: any) => {
        router.push({
          name: 'associatPile',
          query: { id: row.id },
        });
      },
    },
    {
      label: '删除',
      permission: 'courtsList.delete',
      color: '#FA3939',
      handle: (row: any) => {
        afterDelPlatformAreaById({
          id: row.id,
        }).then((res: any) => {
          if (res === '0') {
            ElMessageBox.confirm(
              '当前台区有关联电桩，删除台区会解除关联关系，是否继续删除？',
              '提示',
              {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
              }
            ).then(() => {
              delPlatformAreaById({
                id: row.id,
              }).then((res: any) => {
                tableRef.value.getData();
              });
            });
          } else if (res === '1') {
            ElMessageBox.confirm('此操作将永久删除该数据，是否继续？', '提示', {
              confirmButtonText: '确认',
              cancelButtonText: '取消',
              type: 'warning',
            }).then(() => {
              delPlatformAreaById({
                id: row.id,
              }).then((res: any) => {
                tableRef.value.getData();
              });
            });
          }
        });
      },
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'courtsList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'courtsAdd',
        });
      },
    },
  ],
  query: [
    {
      label: '台区编码',
      prop: 'platformAreaCode',
      comName: 'input',
      props: {
        placeholder: '请输入台区编码',
      },
    },
    {
      label: '台区名称',
      prop: 'platformAreaName',
      comName: 'input',
      props: {
        placeholder: '请输入台区名称',
      },
    },
  ],
});
</script>